package com.briup.estore.dao.impl;

import com.briup.estore.common.pojo.ShopCar;
import com.briup.estore.common.utils.JDBCUtils;
import com.briup.estore.dao.ShopCarDao;

import java.util.List;

import static com.briup.estore.common.utils.JDBCUtils.DML;
import static com.briup.estore.common.utils.JDBCUtils.findOne;

/**
 * @Author briup-adam
 * @Date 2023/8/3 下午3:03
 * @Description 对表【es_shopcar】的数据库操作
 **/

public class ShopCarDaoImpl implements ShopCarDao {
    @Override
    public List<ShopCar> findByUserId(Integer userId) {
        List<ShopCar> list = JDBCUtils.findMany("select id id,user_id ,book_id, num from es_shopcar  where user_id=? ;", ShopCar.class, userId);
        return list;
    }

    @Override
    public ShopCar findByUserIdAndBookId(Integer userId, Integer bookId) {
        ShopCar car = JDBCUtils.findOne("select id id,user_id ,book_id, num from es_shopcar  where user_id=? and book_id=?;", ShopCar.class, userId, bookId);
        return car;
    }

    @Override
    public void updateNumById(Integer num, Integer id) {
        DML("update es_shopcar set num=? where id=?;", num, id);

    }

    @Override
    public void save(Integer userId, Integer bookId) {
        DML("insert into es_shopcar(book_id,user_id,num) values(?,?,1);", bookId, userId);
    }

    @Override
    public void deleteById(Integer id) {
        DML(" delete from es_shopcar where id=?;", id);
    }

    @Override
    public ShopCar findById(Integer id) {
        ShopCar shopCar = findOne("select   id,user_id ,book_id, num from es_shopcar  where id=?",ShopCar.class,id);
        return  shopCar;
    }
}
